package user.controller;

import core.constants.Constant;
import core.constants.HttpConstants;
import core.controller.BaseController;
import core.domain.Result;
import core.domain.vo.LoginUserVO;
import core.threadLocal.ThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import user.domain.user.dto.UserDTO;
import user.domain.user.dto.UserEditDTO;
import user.domain.user.vo.UserDetailVO;
import user.service.IUserService;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserDTO userDTO) {
        log.info("UserController -> sendCode: {}", userDTO);
        return toResult(userService.sendCode(userDTO));
    }

    @PostMapping("/code/login")
    public Result<String> codeLogin(@RequestBody UserDTO userDTO) {
        log.info("UserController -> codeLogin: {}", userDTO);
        return Result.success(userService.codeLogin(userDTO.getPhone(), userDTO.getCode()));
    }

    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("UserController  logout(): {}", token);
        return toResult(userService.logout(token));
    }

    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        log.info("UserController  info(): {}", token);
        return userService.info(token);
    }

    @GetMapping("/detail")
    public Result<UserDetailVO> detail(){
        log.info("UserController  detail()");
        return Result.success(userService.detail());
    }

    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody UserEditDTO userEditDTO){
        log.info("UserController  edit(): {}", userEditDTO);
        return toResult(userService.edit(userEditDTO));
    }

    @PostMapping("/head-image/update")
    public Result<Void> updateHeadImage(@RequestParam("headImage") String headImage){
        log.info("UserController  updateHeadImage(): {}", headImage);
        return toResult(userService.updateHeadImage(headImage));
    }
}
